package org.metasketch.editor.toolbox.advanced;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.metasketch.model.CommonIdentified;

public class DropHandlerRegistry {

	Map<Class, IRealDropHandler> dropHandlerPerClass = new HashMap<Class, IRealDropHandler>();

	Map<Class, IRealDropHandler> inheritedDropHandlerPerClass = new HashMap<Class, IRealDropHandler>();

	public void register(Class type, IRealDropHandler toolHandler) {
		dropHandlerPerClass.put(type, toolHandler);
	}

	public IRealDropHandler get(Class type) throws Throwable {
		return dropHandlerPerClass.get(type);
	}

	public IRealDropHandler get(CommonIdentified semantic) throws Throwable {
		return get(semantic.getClass());
	}

	public void registerInherited(Class type, IRealDropHandler toolHandler) {
		inheritedDropHandlerPerClass.put(type, toolHandler);
	}

	public IRealDropHandler getInherited(Class type) throws Throwable {
		Set<Class> keys = inheritedDropHandlerPerClass.keySet();
		for (Class class1 : keys) {
			if (class1.isAssignableFrom(type))
				return inheritedDropHandlerPerClass.get(class1);
		}

		return null;
	}

	public IRealDropHandler getInherited(CommonIdentified semantic)
			throws Throwable {
		return getInherited(semantic.getClass());
	}

}
